package com.beta.dto.product;

import com.beta.em.product.SaleStatus;
import com.beta.support.ProductPropertiesUtil;
import com.beta.support.ResourceImagePathBuilder;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by lichao on 2016/12/20.
 */
@Getter
@Setter
public class ProductItemDto implements Serializable {

    private static final long serialVersionUID = -8947232427516135450L;

    private Integer id;

    private String sku;

    private Integer productId;

    private String name;

    private BigDecimal price;

    private BigDecimal quantity;

    private Integer userId;

    private String userName;

    private Integer stockLocationId;

    private String productDesc;

    private String specDesc;

    private String cityCode;

    private String cityName;

    private Integer minQtyOnSell;

    private Integer maxQtyOnSell;

    private String chargeUnit;

    private String productPoster;

    private Integer assetId;

    private String assetImage;

    private String saleStatus;

    private String slideImgPath1;
    private String slideImgPath2;
    private String slideImgPath3;
    private String slideImgPath4;
    private String slideImgPath5;
    private String slideImgPath6;
    private String slideImgPath7;
    private String slideImgPath8;

    private List<ProductPropertiesDto> properties;

    private List<ProductItemOptions> options;

    public String getPosterUrl() {
        if (StringUtils.isNoneBlank(getAssetImage())) {
            return ResourceImagePathBuilder.assetImagePath(this.assetId, this.assetImage);
        }
        return ResourceImagePathBuilder.productPosterPath(getProductId(), getProductPoster());
    }

    public String getOrigin() {
        return ProductPropertiesUtil.getOrigin(properties);
    }

    public String getLevel() {
        return ProductPropertiesUtil.getLevel(options);
    }

    public List<String> getSlideImages() {
        List<String> slideImgUrls = new ArrayList<>();
        try {
            int lastSliderImagePathIndex = 0;
            for (int index = 1; index <= 8; ++index) {
                String slidePath = getSlideImgPath(index, BeanUtils.getProperty(this, "slideImgPath" + index));
                if (StringUtils.isNotBlank(slidePath)) {
                    lastSliderImagePathIndex = index;
                }
                slideImgUrls.add(slidePath);
            }
            if (lastSliderImagePathIndex == 0) {
                return Collections.emptyList();
            } else {
                return slideImgUrls.subList(0, lastSliderImagePathIndex);
            }
        } catch (Exception e) {
            // ignore
            return Collections.emptyList();
        }
    }

    public String getSaleStatusText() {
        return SaleStatus.getCnValue(this.saleStatus);
    }

    private String getSlideImgPath(Integer sliderIndex, String slideImgPath) {
        return ResourceImagePathBuilder.productSlideImagePath(getId(), sliderIndex, slideImgPath);
    }

}
